layui.use(['layer','form','table','element','layedit','upload'],function () {
    var layer = layui.layer
        ,form = layui.form
        ,table =layui.table
        ,element = layui.element
        ,layedit = layui.layedit
        ,upload = layui.upload;

    var projectTitle = $("#projectTitle").val();

    var E = window.wangEditor
    var editor = new E('#addEdit')
    // 或者 var editor = new E( document.getElementById('editor') )
    editor.customConfig.uploadImgServer = '/admin/project/uploadProjectImg2'
    editor.customConfig.uploadFileName = 'file'
    // 自定义菜单配置
    editor.customConfig.menus = [
        'head',  // 标题
        'fontSize',  // 字号
        'foreColor',  // 文字颜色
        'backColor',  // 背景颜色
        'link',  // 插入链接
        'justify',  // 对齐方式
        'quote',  // 引用
        'image',  // 插入图片
        'undo',  // 撤销
        'redo',  // 重复
        'qsy'  // 去水印
    ]
    editor.create();
    editor.txt.html($("#content").val());

    form.on("radio(typeChange)", function(data){
        var value = data.value;
        if(value == '1') {
            $("#topLabelDiv").attr("style","display:unset");
        }else {
            $("#topLabelDiv").attr("style","display:none");
        }
    });

    //表单验证
    form.verify({
        id:function (value) {
            if(value == ""){
                return "id不能为空";
            }
        },
        cover:function (value) {
            // if(value == ""){
            //     return "封面不能为空";
            // }
        },
        content:function () {
            var content = editor.txt.text();
            if(content == "" || content.length==0){
                return "内容不能为空";
            }
        }
    });

        //提交表单
    form.on('submit(form)',function (data) {

        //校验类型和标签是否选择
        // var label_length = $("input[name=label]:checked").length;
        // var type_length = $("input[name=type]:checked").length;
        layer.msg('保存中...', { icon: 16, shade: [0.5, '#f5f5f5'], scrollbar: false, offset: '50%', time: 300000 });

        var summary = editor.txt.text().substring(0,108)+"...";

        var types = "";

        $("input[name=type]:checked").each(function () {
            types = types + "," + $(this).val();
        });

        if(types == ""){
            layer.msg("请选择项目类型");
            return;
        }

        var keyword = $("#keyword").val();
        if(keyword != null && keyword != '') {
            keyword = keyword.replaceAll("，",",");
            keyword = keyword.replaceAll("、",",");
        }

        types = types.substr(1,types.length-1);

        var paramData = {
            "title":$("#title").val(),
            "summary":summary,
            "content":editor.txt.html(),
            "types":types,
            "sourceURL":$("#sourceURL").val(),
            "isHomePage":$('input[name="isHomePage"]:checked ').val(),
            "isNofollow":0,
            "keyword":keyword,
            "spiderProjectId":$("#spiderProjectId").val(),
            "isTemp":$('input[name="isTemp"]:checked ').val(),
            "profit":$("#profit").val(),
            "projectName":$("#projectName").val(),
        };

        var data2 = JSON.stringify(paramData);

        //保存项目
        $.ajax({
            url:"/admin/project/free_project_save_bySpider",
            type:"POST",
            data:data2,
            // dataType:"json",
            contentType:"application/json;charset=utf-8",
            success:function (result) {
                // console.log("data=="+form_data)
                if(result.flag){
                    layer.msg(result.message);
                    //清空内容
                    // $("#form").reset();
                    document.getElementById("form").reset();
                    layui.form.render();
                }else{
                    layer.msg(result.message);
                }
            },
            error:function () {
                layer.msg("系统错误！");
            }
        });

    });

    // layedit.setContent(index, "<h1 style='color: red'>hahahha</h1>");

    // $('#submitBtn').click(function () {
    //     //获取编辑器的值
    //     var content = layedit.getContent(index);
    //
    //     alert(content);
    // });
    $("#wycBtn").bind("click",function () {
        var content = editor.txt.html();
        $.ajax({
            url:"/admin/system/wyc",
            type:"POST",
            data:{"content":content},
            success:function (result) {
                if(result.flag){
                    editor.txt.html(result.data.newContent) // 重新设置编辑器内容
                }else{
                    layer.msg(result.message);
                }
            },
            error:function () {
                layer.msg("系统错误！");
            }
        });
    })

});


function toBaiduInex() {
    var title = $("#title").val();
    window.open("https://index.baidu.com/v2/main/index.html#/demand/"+title+"?words="+title);
}


function saveLabel(typeId) {
    var labelName = $("#addLabel").val();

    //保存项目
    $.ajax({
        url:"/admin/project/saveLabel?labelName="+labelName+"&typeId="+typeId,
        type:"POST",
        dataType:"json",
        contentType:"application/json;charset=utf-8",
        success:function (result) {
            // console.log("data=="+form_data)
            if(result.flag){
                layer.msg(result.message);
                //清空内容
                // $("#form").reset();
                // document.getElementById("form").reset();
                location.reload();

            }else{
                layer.msg(result.message);
            }
        },
        error:function () {
            layer.msg("系统错误！");
        }
    });

}


function selectTopLabel(labelName) {
    var keyword = $("#keyword").val();
    if(keyword == null || keyword == '') {
        keyword = labelName
    }else {
        keyword = keyword + ',' +labelName;
    }
    $("#keyword").val(keyword)
}
